package com.zw.yzk.kabu.view.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.MyFansAdapter;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.internal.component.DaggerMyFansComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.FansListModule;
import com.zw.yzk.kabu.internal.module.FollowModule;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.model.MyFansModel;
import com.zw.yzk.kabu.presenter.MyFansPresenter;
import com.zw.yzk.kabu.view.view.MyFansView;
import com.zw.yzk.refresh.library.PullToRefreshLayout;
import com.zw.yzk.refresh.library.RefreshBuilder;
import com.zw.yzk.refresh.library.State;
import com.zw.yzk.refresh.library.loadmore.DefaultLoadMoreViewCreator;
import com.zw.yzk.refresh.library.refrsh.DefaultRefreshViewCreator;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;

/**
 * Created by zhanwei on 2017/2/11.
 */

public class MyFansActivity extends BaseActivity implements MyFansView, MyFansAdapter.FollowClickedListener {

    @Bind(R.id.refresh_view)
    PullToRefreshLayout refreshLayout;
    @Bind(R.id.fans_list)
    RecyclerView fansList;
    @Bind(R.id.empty)
    View empty;

    @Inject
    MyFansPresenter presenter;
    private MyFansAdapter adapter;
    private boolean followAction;
    private int index;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_fans;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.my_fans))
                .build();
    }

    @Override
    public void initComponent() {
        DaggerMyFansComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .fansListModule(new FansListModule())
                .followModule(new FollowModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initRecyclerView();
        getFansList(0);
        LoadingDialogManager.getInstance().showLoadingDialog(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }

    @Override
    public void getFansListSuccess(List<MyFansModel> list) {
        if (refreshLayout.isLoading()) {
            adapter.add(list);
            refreshLayout.loadMoreFinish(list.isEmpty() ? State.LOAD_ALL : State.LOADING_SUCCEED);
        } else {
            adapter.refresh(list);
            refreshLayout.refreshFinish(State.REFRESH_SUCCEED);
        }

        refreshEmptyStatus(adapter.getItemCount() == 0);
    }

    @Override
    public void getFansListFail() {
        refreshLayout.finishRefreshingOrLoadingStatus(false);
        refreshEmptyStatus(adapter.getItemCount() == 0);
    }

    @Override
    public boolean getFollowState() {
        return followAction;
    }

    @Override
    public void followComplete(boolean success) {
        adapter.getDataList().get(index).setHasConcerned(followAction);
        adapter.notifyItemChanged(index);
    }

    @Override
    public void onClicked(int position) {
        followAction = !adapter.getDataList().get(position).isHasConcerned();
        index = position;

        //关注或者取消关注
        follow(adapter.getDataList().get(position).getId(), followAction);
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {
        fansList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new MyFansAdapter(null);
        adapter.setFollowClickedListener(this);
        fansList.setAdapter(adapter);
        new RefreshBuilder.Builder(refreshLayout)
                .setRefreshViewCreator(new DefaultRefreshViewCreator())
                .setLoadMoreViewCreator(new DefaultLoadMoreViewCreator())
                .setRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                        getFansList(0);
                    }

                    @Override
                    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                        getFansList(adapter.getItemCount());
                    }
                }).build();
    }

    /**
     * 获取粉丝列表
     *
     * @param start
     */
    private void getFansList(int start) {
        presenter.getFansList(start);
    }

    /**
     * 是否显示空提示
     *
     * @param show true:显示，false:不显示
     */
    private void refreshEmptyStatus(boolean show) {
        empty.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    /**
     * 关注或者取消关注
     *
     * @param id    被关注者ID
     * @param state true：关注，false：取消关注
     */
    private void follow(int id, boolean state) {
        presenter.follow(id, state);
    }
}
